package com.hilaver.dzmis.exhibition.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.exhibition.hibernate.Yarn;
import com.hilaver.dzmis.exhibition.service.YarnService;
import com.hilaver.dzmis.exhibition.utils.Constants;

/**
 * Servlet implementation class for Servlet: YarnServlet
 *
 */
 public class YarnServlet extends AbstractBaseServlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public YarnServlet() {
		this.fullName = Yarn.class.getName();
		this.simpleName = Yarn.class.getSimpleName();
	}   	
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);

		YarnService serviceObj = new YarnService();
		Method method = null;
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAllPagination",
					new Class[] { int.class, String.class, String.class });// int
			// page,
			// String
			// sort,
			// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir });
			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			method = serviceObj.getClass().getMethod("edit",
					new Class[] { Object.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.obj });
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			method = serviceObj.getClass().getMethod("delete",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			method = serviceObj.getClass().getMethod("get",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setPaginationParam(request);
			this.setFilterParam(request);
			method = serviceObj.getClass().getMethod(
					"getAllPagination",
					new Class[] { int.class, String.class, String.class,
							String[].class });// int page, String sort, String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir, this.filters });
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			this.setPaginationParam(request);
			rtn = serviceObj.getAll(this.sort, this.dir);
			break;
		case ACTION_GET_ALL_FROM_CHART:
			this.setId(request);
			rtn = serviceObj.getAllFromTechnicChartId(this.id);
			break;
		}

		return rtn;
	}

}